﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BidFunLib.BidWeb;
using BidFunLib.BidWeb.Sale;
using BidFunSpy.BidWeb;


namespace BidFunSpy
{
    public partial class SpyForm : Form
    {
        public BidWebRootSpy Manager { get; set; }

        Timer myTimer = new Timer();

        public SpyForm()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();

            Manager = new BidWebRootSpy();
            Manager.SelectedBidObjectForSpy = BidFunLib.BidSpyConfigHelper.Instance.getList();
            Manager.AllSales = BidFunLib.BidSpyConfigHelper.Instance.getAllSales();

            Manager.Start();
            Manager.OnBidListUpdate += new BidFunLib.BidWeb.Sale.OnBidListUpdateEventHandler(Manager_OnBidListUpdate);
            myTimer.Interval = 500;

            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Start();


        }

        void myTimer_Tick(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                BidWebSale sale = item.Tag as BidWebSale;
                if (sale.Span_time != null)
                {
                    item.SubItems[2].Text = sale.Span_time;
                }

                if (sale.LastBidAmount != null)
                {
                    item.SubItems[3].Text = sale.LastBidAmount;
                }
                item.SubItems[4].Text = (sale.LastUserName);
                item.SubItems[5].Text = (sale.NbBids.ToString());
            }         
        }

        void Manager_OnBidListUpdate()
        {
            myTimer.Stop();
            listView1.Items.Clear();
            foreach (BidWebSale bid in Manager.SaleWebManager.Map.Values)
            {
                if (bid.IsEnded == false)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = bid.Sale.BidObject.Label;
                    item.Tag = bid;

                    item.SubItems.Add(bid.Sale.BidFunIdentifier);
                    if (bid.Span_time != null)
                    {
                        item.SubItems.Add(bid.Span_time);
                    }
                    else
                    {
                        item.SubItems.Add("");
                    }
                    if (bid.LastBidAmount != null)
                    {
                        item.SubItems.Add(bid.LastBidAmount);
                    }
                    else
                    {
                        item.SubItems.Add("");
                    }
                    item.SubItems.Add(bid.LastUserName);
                    item.SubItems.Add(bid.NbBids.ToString());

                    item.SubItems.Add(bid.Sale.BidFunBotAuthorized.ToString());
                    item.SubItems.Add(bid.Sale.BeginnerOnly.ToString());

                    listView1.Items.Add(item);
                }
            }
            myTimer.Start();
        }

        private void historyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BidsHistory bidsHistory = new BidsHistory(Manager);
            bidsHistory.Show(this);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            ListViewItem item = this.listView1.SelectedItems[0];
            if (item != null)
            {
                CurrentBidDetail detail = new CurrentBidDetail((BidWebSale)item.Tag);
                detail.Show(this);
            }
        }
    }
}
